package com.ly.user_system.base;

import com.ly.user_system.entity.BaseSysUser;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.List;


public class BaseUserDetails implements UserDetails {

    private BaseSysUser user;

    // 目前权限以rbac进行处理
    private List<SimpleGrantedAuthority> authorities;

    public BaseUserDetails(BaseSysUser user, List<SimpleGrantedAuthority> authorityList) {
        this.user=user;
        this.authorities=authorityList;
    }

    public Long getUserId(){
        return user.getId();
    }


    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return authorities;
    }

    @Override
    public String getPassword() {
        return user.getPassword();
    }

    @Override
    public String getUsername() {
        return user.getUsername();
    }

    public Boolean adminFlag(){
        return "admin".equals(user.getUsername());
    }

    /**
     * 账号是否过期
     * 场景:临时账号
     */
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    /**
     * 账号是否被锁
     * 场景：连续输入错误
     */
    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    /**
     * password是否过期
     * 密钥定期重置
     */
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return user.getStatus()==1;
    }
}
